﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using AgencyEntityLib.BaseEntity;
using Ecopsy.DataTier;

public partial class iFarmeAdmin_EditSiteMap : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack) MyDataBind();

    }

    private void MyDataBind()
    {
        ///chRoleList.DataSource = GetEmplRoles();////Чудо блять!!!
        ListItemCollection coll = GetEmplRoles();
        chRoleList.Items.Clear();
        foreach (ListItem i in coll)
        {
            ListItem it = new ListItem(i.Text, i.Value);
            it.Selected = i.Selected;
            chRoleList.Items.Add(it);
        }

        coll = GetPageTypes();
        rbPageType.Items.Clear();
        foreach (ListItem i in coll)
        {
            ListItem it = new ListItem(i.Text, i.Value);
            it.Selected = i.Selected;
            rbPageType.Items.Add(it);
        }

        coll = GetKeys();
        cmbKeys.Items.Clear();
        foreach (ListItem i in coll)
        {
            ListItem it = new ListItem(i.Text, i.Value);
            it.Selected = i.Selected;
            cmbKeys.Items.Add(it);
        }

        DataBind();
    }

    private ListItemCollection GetPageTypes()
    {
        DictionaryItemList l = new DictionaryItemList();
        l.DictionaryTypeID = "D5AB6916-BDA5-4FEC-9CC2-CD2F15A1F278";
        l.Load();
        ListItemCollection res = new ListItemCollection();
        foreach (DictionaryItem i in l)
        {
            ListItem it = new ListItem(i.Value.ToString (),i.ID);
            if (SiteMapItem.PageType!=null) it.Selected = (i.ID ==SiteMapItem.PageType) ;
            res.Add(it);
        }
        return res;
    }
    
    private ListItemCollection GetKeys()
    {
        DictionaryItemList l = new DictionaryItemList();
        l.DictionaryTypeID = "85B6EB06-EBC1-4CA7-ABA4-77291272ED9A";
        l.Load();
        ListItemCollection res = new ListItemCollection();
        foreach (DictionaryItem i in l)
        {
            if (Agency.SitMap.FindItem(i.ID) != null & i.ID != SiteMapItem.Key) continue;   
            ListItem it = new ListItem();
            it.Value = i.ID;
            it.Text = i.Value.ToString ();
            if (SiteMapItem.Key != null) it.Selected = (i.ID == SiteMapItem.Key);
            res.Add(it);
        }
        return res;
    }

    /// <summary>
    /// Возвращает listItem с писок ролей с отметкой участия 
    /// </summary>
    /// <returns></returns>
    private ListItemCollection GetEmplRoles()
    {
        ListItemCollection res = new ListItemCollection();

        ////список ролей
        ws_Securety ws = new ws_Securety();
        RoleList rl= ws.GetRoleList();          
        foreach (DictionaryItem item in rl)
        {
            ListItem i = new ListItem();
            i.Value = item.ID;
            i.Text = item.Value.ToString();
            i.Selected = (null != SiteMapItem.Roles[item.ID]);
            res.Add(i);
        }
        return res;
    }

    private void ReadForm()
    {
        SiteMapItem.RedirectUrlValue = txtRedirect.Text;
        SiteMapItem.Key  = cmbKeys.SelectedItem.Value ;
        SiteMapItem.Describe = txtComment.Text;
        

        SiteMapItem.Roles.Clear();
        foreach (ListItem item in chRoleList.Items)
        {
            if (!item.Selected) continue;
            EntitySingle s = new EntitySingle() { ID = item.Value };
            SiteMapItem.Roles.Add(s);   
        }

        foreach (ListItem item in rbPageType.Items)
        {
            if (!item.Selected) continue;            
            SiteMapItem.PageType =item.Value ;
        }


    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        ReadForm();
        wsAgency ws = new wsAgency();
        string id = Agency.ID;
        ws.SaveAgency(Agency);        

    }

    protected bool IsNewRecord
    {
        get { return SiteMapItemID.IndexOf("NEW_RECORD") > -1; }
    }
    
    protected string SiteMapItemID { get { return Request.QueryString["SiteMapItemID"]; } }

    protected SiteMapItem SiteMapItem
    {
       get
       {
           wsAgency ws = new wsAgency();
           if (null == Agency.SitMap[SiteMapItemID]) 
                Agency.SitMap.Add(new SiteMapItem() {ID =SiteMapItemID,  Key = SiteMapItemID });
           return   (SiteMapItem)Agency.SitMap[SiteMapItemID];   
           
           
       }
    }

    protected Agency Agency 
    {
        get {
            wsAgency ws = new wsAgency();
            return ws.GetAgency(AgencyID);

        }
    }

    protected string AgencyID { get { return Request.QueryString["AgencyID"]; } }
}
